package com.dingreading.cloud.admin.service.impl;

import com.dingreading.cloud.admin.entity.WebImage;
import com.dingreading.cloud.admin.entity.table.WebImageTableDef;
import com.dingreading.cloud.admin.mapper.WebImageMapper;
import com.dingreading.cloud.admin.service.WebImageService;
import com.dingreading.cloud.common.util.NepUtil;
import com.dingreading.cloud.common.util.PageUtil;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryCondition;
import com.mybatisflex.core.query.QueryMethods;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.update.UpdateChain;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

/**
 * 服务层实现。
 *
 * @author Administrator
 * @since 2023-11-22
 */
@Service
public class WebImageServiceImpl extends BaseServiceImpl<WebImageMapper, WebImage, WebImageTableDef> implements WebImageService {

    @Override
    protected WebImageTableDef getTable() {
        return WebImageTableDef.webImage;
    }


    @Override
    public boolean updateEnabled(Long id, Integer enabled) {
        return UpdateChain.of(WebImage.class)
                .set(table.enabled, enabled)
                .where(table.id.eq(id))
                .update();
    }

    @Override
    public Page<WebImage> pageList(PageUtil pageUtil, String keyword, Integer enabled, Integer typeId) {
        QueryCondition condition = QueryCondition.createEmpty();
        if (StringUtils.isNotBlank(keyword))
            condition.and(table.type.like(keyword)
            );
        if (null != enabled)
            condition.and(table.enabled.eq(enabled));
        if (null != typeId)
            condition.and(table.typeId.eq(typeId));

        QueryWrapper wrapper = QueryWrapper.create()
                .where(condition)
                .orderBy(table.orderNum.asc());

        return getMapper().paginate(Page.of(pageUtil.getPage(), pageUtil.getRows()), wrapper);
    }

    @Override
    public int getMaxShowOrder(Integer typeId) {
        QueryWrapper wrapper = QueryWrapper.create()
                .select(QueryMethods.max(table.orderNum))
                .from(table)
                .where(table.typeId.eq(typeId));

        Integer integer = getMapper().selectObjectByQueryAs(wrapper, Integer.class);
        return NepUtil.nullToZero(integer);
    }

    @Override
    public WebImage getGtShowOrder(Integer typeId, Integer orderNum) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.typeId.eq(typeId))
                .and(table.orderNum.gt(orderNum));

        QueryWrapper wrapper = QueryWrapper.create()
                .where(condition)
                .orderBy(table.orderNum.asc());

        return getMapper().selectOneByQuery(wrapper);
    }

    @Override
    public WebImage getLtShowOrder(Integer typeId, Integer orderNum) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.typeId.eq(typeId))
                .and(table.orderNum.lt(orderNum));

        QueryWrapper wrapper = QueryWrapper.create()
                .where(condition)
                .orderBy(table.orderNum.desc());

        return getMapper().selectOneByQuery(wrapper);
    }

    @Override
    public void updateOrderNum(Long id, Integer orderNum) {
        UpdateChain.of(WebImage.class)
                .set(table.orderNum, orderNum)
                .where(table.id.eq(id))
                .update();
    }

    @Override
    public int dropBean(Long id) {
        return getMapper().deleteByCondition(table.id.eq(id));
    }
}
